package cn.sciento.core.user;


import cn.sciento.core.oauth.CustomUserDetails;
import cn.sciento.core.oauth.DetailsHelper;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public abstract class UserType {
    public static final String PARAM_NAME = "user_type";
    public static final String DEFAULT_USER_TYPE = "P";
    private static final Map<String, UserType> HOLDER = new HashMap();

    public UserType() {
        HOLDER.put(this.value(), this);
    }

    public abstract String value();

    public static UserType ofDefault() {
        return HOLDER.get("P");
    }

    public static UserType ofDefault(String userType) {
        if (StringUtils.isBlank(userType)) {
            CustomUserDetails self = DetailsHelper.getUserDetails();
            if (self != null) {
                userType = self.getUserType();
            }
        }

        if (StringUtils.isBlank(userType)) {
            return ofDefault();
        } else {
            if (!HOLDER.containsKey(userType)) {
                String finalUserType = userType;
                UserType newUserType = new UserType() {
                    public String value() {
                        return finalUserType;
                    }
                };
                HOLDER.put(userType, newUserType);
            }

            return HOLDER.get(userType);
        }
    }
}
